package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.EbookEntry;
import com.qf.mapper.EbookEntryDao;
import com.qf.service.EbookEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/10/4 15:14
 */
@Service
public class EbookEntryServiceImpl implements EbookEntryService {
    @Autowired
    private EbookEntryDao ebookEntryDao;

    @Override
    public List<EbookEntry> findAll() {
        return ebookEntryDao.selectByExample(null);
    }

    @Override
    public List<EbookEntry> findAllByCId(Integer id) {
        return ebookEntryDao.selectByCId(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void add(EbookEntry ebookEntry) {
        int i = ebookEntryDao.insert(ebookEntry);
        if (i != 1){
            System.out.println("添加失败");
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void update(EbookEntry ebookEntry) {
        int i = ebookEntryDao.updateByPrimaryKeySelective(ebookEntry);
        if (i != 1){
            System.out.println("修改失败");
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void delete(Integer id) {
        int i = ebookEntryDao.deleteByPrimaryKey(id);
        if (i != 1){
            System.out.println("删除失败");
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public PageInfo<EbookEntry> findByCIdAndPage(Integer num, Integer size, Integer id) {

        //1.分页
        PageHelper.startPage(num,size);

        //2.查询
        List<EbookEntry> entryList = ebookEntryDao.selectByCId(id);

        //3.封装PageInfo
        PageInfo<EbookEntry> pageInfo = new PageInfo<>(entryList);

        //4.返回
        return pageInfo;
    }
}
